/**
 * 注册校验
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2014-09-05 11:10:42
 * @update  2014-09-05 11:10:42
 * @for register.jsp
 * @version $Id$
 */
var BSTYUN = BSTYUN || {};
BSTYUN.check = {
    //IE6判定
    isIE6: window.VBArray && !window.XMLHttpRequest,
    //判断是否为中英文，数字，下划线，减号
    isNick: function(str) {
        var nickReg = /^[\u4e00-\u9fa5A-Za-z0-9-_]+$/;
        return nickReg.test(str);
    },
    //判断邮箱
    isEmail: function(str) {
        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
        return emailReg.test(str);
    },
    //判断手机
    isMobile: function(str) {
        var mobileReg = /^1[345678][0-9]{9}$/;
        return mobileReg.test(str);
    },
    // 判断固话
    isTelephone: function(str) {
        var phoneReg = /^0\d{2,3}-\d{5,9}$/;
        return phoneReg.test(str);
    },
    //判断URL
    isUrl: function(str) {
        var urlReg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$/;
        return urlReg.test(str);
    },
    //判断数字
    isNum: function(str) {
        var numReg = /^[0-9]\d*$/;
        return numReg.test(str);
    }
};
BSTYUN.forgetPassword = {
    $inputs: null,
    flag: {
        email: false,
        imageCode: false
    },
    _timer: null,
    /*全局定时器*/
    init: function() {
        var that = this;
        // 切换tab
        that.switchTab();
        //手机检测
        that.phoneCheck();
        //邮箱检测
        that.emailCheck();
        //图片校验码校验
        that.codeCheck();
        //图片校验码失去焦点
        that.codeBlur();
        //提交
        that.submitFind();
        //获取手机验证码
        that.getEVerifyCode();
        //获取邮箱验证码
        that.getPVerifyCode();
        /*图片验证码替换*/
        that.changePicCode();
    },
    // 切换tab
    switchTab: function(){
    	var $radio = $("input[name=RadioGroup1]"),
    		$phonedl = $("#phonedl") ,
    		$pic_code = $("#pic_code") ,
    		$emaildl = $("#emaildl") ;
    	$radio.on("click",function(){
    		var $this = $(this),
    			id = $.trim( $this.attr("id") );
    		switch ( id ){
    			case "phoneFind": 
    				$phonedl.show();
    				$pic_code.show();
    				$emaildl.hide();
    				break;
    			default:
    				$phonedl.hide();
    				$pic_code.hide();
					$emaildl.show();
    				break;
    		}
    		
    	});
    },
    //图片校验码失去焦点
    codeBlur: function() {
    	$("#img_code").on("blur",function(){
    		var flag = BSTYUN.forgetPassword.codeCheck();
    		if(!flag) {
    			BSTYUN.dialog.warn("图片验证码不正确");
				return ;
    		}
    	})
    },
    //图片校验码校验
    codeCheck: function() {
		var img_code = $.trim( $("#img_code").val() );
		var flag = false
		if (img_code == null || img_code == '' ) {
			flag = false;
		}else {
			/*ajax判断验证码是否正确*/
			$.ajax({
				type: "post",
				url: "/user/checkImgCode",
				data: {imageCode:img_code},
				async:false,
				success: function(data){
					flag = data;
				}
			});
		}
		return flag;
    },
    //手机检测
    phoneCheck: function() {
    	$("#phone").on("blur",function(){
    		var phone = $.trim( $("#phone").val() );
    		if (phone == '' || phone.length == 0) {
    			return ;
    		}else if (!BSTYUN.check.isMobile(phone)) {
    			return BSTYUN.dialog.warn("手机号码格式不正确");
    		}else {
    			/*ajax判断手机号码是否存在*/
    			$.ajax({
    				cache: false,
    				type: "POST",
    				url: "/user/bound/selectUserPsdByBoundTel?random=" + Math.random(),
    				data: {
    					boundTel: phone
    				},
    				async: false,
    				success: function(data) {
    					if (data == true) {
    						BSTYUN.dialog.warn('该手机未绑定');
    					}
    				},
    				error: function(request) {
    					BSTYUN.dialog.ok("请求失败！请重试");
    					return;
    				}
    			});
    		}
    	})
    },
    //邮箱检测
    emailCheck: function() {
    	$("#email").on("blur",function(){
	    	var email = $.trim( $("#email").val() );
	    	if (email == '' || email.length == 0) {
	    		return ;
	        }else if (!BSTYUN.check.isEmail(email)) {
	        	return BSTYUN.dialog.warn("邮箱格式不正确");
	        }else {
	            /*ajax判断用户名是否存在*/
	            $.ajax({
	                cache: false,
	                type: "POST",
	                url: "/user/bound/selectUserPsdByBoundEmail?random=" + Math.random(),
	                data: {
	                    boundEmail: email
	                },
	                async: false,
	                success: function(data) {
	                    if (data == true) {
	                    	BSTYUN.dialog.warn('该邮箱未绑定');
	                    }
	                },
	                error: function(request) {
	                	BSTYUN.dialog.ok("请求失败！请重试");
	                    return;
	                }
	            });
	        }
    	})
    },
    //获取手机验证码
    getPVerifyCode: function(){
    	$("#phoneCode").on("click",function(){
    		if(BSTYUN.forgetPassword.codeCheck()) {
    			var phone = $.trim( $("#phone").val() );
        		if (phone == '' || phone.length == 0) {
    	    		return BSTYUN.dialog.warn('请填写正确的手机号码！');
    	        }
            	$.ajax({
                    cache: false,
                    type: "POST",
                    url : "/user/findPassword/getPVerifyCode",
                    data: {
                    	phone: phone  
                    },
                    async: false,
                    success: function(data) {
                        if (data.isSuccess) {
                        	BSTYUN.dialog.ok('验证码发送成功，请注意查收手机短信！');
                        }else{
                        	BSTYUN.dialog.warn('验证码发送失败，请重新点击发送！');
                        }
                    },
                    error: function(request) {
                        BSTYUN.dialog.warn('请求失败！请重试');
                        return false;
                    }
                });
    		}else {
                return BSTYUN.dialog.warn('图片验证码不正确！');;
    		}
    	})
    	
    },
    //获取邮箱验证码
    getEVerifyCode: function(){
    	$("#emailCode").on("click",function(){
	    	var email = $.trim( $("#email").val() );
	    	if (email == '' || email.length == 0) {
	    		return BSTYUN.dialog.warn('请填写正确的邮箱！');;
	        }
	    	$.ajax({
	            cache: false,
	            type: "POST",
	            url : "/user/findPassword/getEVerifyCode",
	            data: {
	            	email: email  
	            },
	            async: false,
	            success: function(data) {
	                if (data.isSuccess) {
	                	BSTYUN.dialog.ok('验证码发送成功，请注意查收邮箱！');
	                }else{
	                	BSTYUN.dialog.warn('验证码发送失败，请重新点击发送！');
	                }
	            },
	            error: function(request) {
	                BSTYUN.dialog.warn('请求失败！请重试');
	                return false;
	            }
	        });
    	})
    },
    //刷新图片验证码
	changePicCode : function() {
		var $change = $("#changeImgCode");
		$change.on('click', function(event) {
			$change.attr('src', '/showImg?random=' + Math.random());
		});
	},
    submitFind: function() {
    	$("#go2submit").on("click",function(){
    		var $radio = $("input[name=RadioGroup1]"),
    			$checked = $radio.filter(":checked"),
    			flg = $checked.val(),
    			text = "手机",
    			account = $.trim( $("#phone").val() );
    		if(flg == "emailFind") {
    			account = $.trim( $("#email").val() );
    			text = "邮箱";
    		}
    		var verifyCode = $.trim( $("#verifyCode").val() );
    		if(verifyCode == '' || verifyCode.length == 0 ){
    			BSTYUN.dialog.warn('请输入验证码！验证码不能为空！');
    		}else{
    			$.ajax({
                    cache: false,
                    type: "POST",
                    url : "/user/findPassword/checkVerifyCode",
                    data: {
                    	verifyCode: verifyCode,
                    	flg:flg,
                    	account:account 
                    },
                    async: false,
                    success: function(data) {
                    	if(data.isVerify){
                    		BSTYUN.dialog.ok('验证码已经被使用！请重新发送验证码！');
                    	}else{
                    		if (data.isSuccess) {
                            	//BSTYUN.dialog.ok('密码找回成功，请注意查收'+text+'！');
                    			location.href="/user/login";
                            }else{
                            	BSTYUN.dialog.warn('验证码输入错误，请重新输入！');
                            }
                    	}
                    },
                    error: function(request) {
                        BSTYUN.dialog.warn('请求失败！请重试');
                        return false;
                    }
                });
    		}
    		
    	})
    }
}
$(function() {
    BSTYUN.forgetPassword.init();
});